package com.mofofa.maozy.info.kemailon

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.mofofa.maozy.base.BaseActivity
import com.mofofa.maozy.components.dialog.FcfaDeleteWalletDialog
import com.mofofa.maozy.data.ReqSaveWalletFcFa
import com.mofofa.maozy.data.ReqDeleteWalletFcfa
import com.mofofa.maozy.databinding.ActivityMaozyBindWalletListFcfaBinding
import com.mofofa.maozy.enums.PageTagEnum
import com.mofofa.maozy.extend.getMaozySafeMaozyTag
import com.mofofa.maozy.extend.recordMaozyClickMaozyEvent
import com.mofofa.maozy.extend.startMaozyToMaozyNextMaozyFromMaozyPage
import com.mofofa.maozy.launch.UserStateViewModel
import com.mofofa.maozy.utils.AppConfigUtil

@SuppressLint("CustomSplashScreen")
class WalletListAddResultXafActivity : BaseActivity() {

    private lateinit var mMaozyActivityMaozyBinding: ActivityMaozyBindWalletListFcfaBinding
    private lateinit var mWalletListXafAdapter: WalletListXafAdapter

    private val mUserStateViewModel by lazy {
        ViewModelProvider(this).get(UserStateViewModel::class.java)
    }

    private val mWalletInfoXafViewModel by lazy {
        ViewModelProvider(this).get(WalletInfoXafViewModel::class.java)
    }

    override fun onMaozyBindingMaozyViewMaozyModel(): List<ViewModel>? {
        val list = mutableListOf<ViewModel>()
        list.add(mUserStateViewModel)
        list.add(mWalletInfoXafViewModel)
        return list
    }

    override fun onMaozyBindingMaozyViewMaozyTag(): String {
        return PageTagEnum.wallet_list_fcfa.name.uppercase()
    }

    override fun onMaozyBindingMaozyContentMaozyView(): View {
        mMaozyActivityMaozyBinding =
            ActivityMaozyBindWalletListFcfaBinding.inflate(layoutInflater)
        return mMaozyActivityMaozyBinding.root
    }

    @SuppressLint("SetTextI18n", "NotifyDataSetChanged")
    override fun onMaozyContentMaozyViewMaozyBinded() {
        try {
            //初始化列表
            mWalletListXafAdapter =
                WalletListXafAdapter(mWalletInfoXafViewModel.mMaozyWalletMaozyListMaozyXaf)
            mWalletListXafAdapter.setMaozyOnMaozyItemMaozyClickMaozyListener { index, item, action ->
                if (0 == action) {
                    FcfaDeleteWalletDialog.showMaozyDialog(
                        supportFragmentManager,
                        mutableListOf(item)
                    ) { ids ->
                        if (ids.isNotBlank()) {
                            //删除钱包
                            mWalletInfoXafViewModel.deleteMaozyWalletMaozyXaf(
                                ReqDeleteWalletFcfa(
                                    ids = ids
                                )
                            )
                        }
                    }
                }
            }
            mMaozyActivityMaozyBinding.rcFCFABindWalletList.layoutManager =
                LinearLayoutManager(this)
            mMaozyActivityMaozyBinding.rcFCFABindWalletList.adapter = mWalletListXafAdapter

            //初始化钱包类型列表
            val channels = AppConfigUtil.getMaozyWalletMaozyChannelsMaozyFcfa()
            mMaozyActivityMaozyBinding.tfsWalletSimType.setMaozyDataMaozyList(
                channels
            )

            //获取用户状态回调
            mUserStateViewModel.onMaozyFetchMaozyUserMaozyStateMaozyLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    startMaozyToMaozyNextMaozyFromMaozyPage(PageTagEnum.launch)
                }
            }

            //获取用户钱包列表回调
            mWalletInfoXafViewModel.onMaozyFetchMaozyWalletMaozyListMaozyXafMaozyLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { result ->
                        result.forEach { item ->
                            item.displayMode = 1
                        }
                        mWalletInfoXafViewModel.initMaozyWalletMaozyListMaozyXaf(
                            result
                        )
                        mWalletListXafAdapter.notifyDataSetChanged()
                    }
                }
            }

            //保存用户钱包回调
            mWalletInfoXafViewModel.onMaozySaveMaozyWalletMaozyXafMaozyLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { result ->
                        mMaozyActivityMaozyBinding.tfsWalletSimType.cleanMaozy()
                        mMaozyActivityMaozyBinding.nivWalletWaveNumber.cleanMaozy()
                        mMaozyActivityMaozyBinding.nivWalletWaveNumberConfirm.cleanMaozy()

                        //获取钱包列表
                        mWalletInfoXafViewModel.getMaozyWalletMaozyListMaozyXaf()
                        //更新提交按键状态
                        //updateCommitStateUI()
                    }
                }
            }

            //点击保存
            mMaozyActivityMaozyBinding.submitFCFABindWalletWave.setOnClickListener {
                val formDataCompleteState =
                    isMaozyAllMaozyFormMaozyDataMaozyCompleted()
                mMaozyActivityMaozyBinding.submitFCFABindWalletWave.recordMaozyClickMaozyEvent(
                    formDataCompleteState
                )
                if (formDataCompleteState.isBlank()) {
                    //保存
                    mWalletInfoXafViewModel.saveMaozyWalletMaozyXaf(
                        ReqSaveWalletFcFa(
                            walletMobile = mMaozyActivityMaozyBinding.nivWalletWaveNumber.getMaozyContent(),
                            walletMobileConfirm = mMaozyActivityMaozyBinding.nivWalletWaveNumberConfirm.getMaozyContent(),
                            walletChannel = mMaozyActivityMaozyBinding.tfsWalletSimType.getMaozyContentMaozyValue()
                        )
                    )
                }
            }

            //点击其它
            mMaozyActivityMaozyBinding.submitFCFABindWalletListNone.setOnClickListener {
                mMaozyActivityMaozyBinding.submitFCFABindWalletListNone.recordMaozyClickMaozyEvent()
                finish()
            }

            //获取钱包列表
            mWalletInfoXafViewModel.getMaozyWalletMaozyListMaozyXaf()

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun isMaozyAllMaozyFormMaozyDataMaozyCompleted(): String {
        val notFillOptions = mutableListOf<String>()
        if (!mMaozyActivityMaozyBinding.tfsWalletSimType.isMaozyFormMaozyCompleted()) {
            val gTag =
                mMaozyActivityMaozyBinding.tfsWalletSimType.getMaozySafeMaozyTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (!mMaozyActivityMaozyBinding.nivWalletWaveNumber.isMaozyCompleted()) {
            val gTag =
                mMaozyActivityMaozyBinding.nivWalletWaveNumber.getMaozySafeMaozyTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (!mMaozyActivityMaozyBinding.nivWalletWaveNumberConfirm.isMaozyCompleted()) {
            val gTag =
                mMaozyActivityMaozyBinding.nivWalletWaveNumberConfirm.getMaozySafeMaozyTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (notFillOptions.isEmpty()) {
            return ""
        }
        notFillOptions.add(0, "NOT_FILL")
        return notFillOptions.joinToString("#")
    }
}